package com.raymond.config.exception;

import com.raymond.entity.commons.DtoResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

import static com.raymond.entity.commons.DtoResult.STATUS_CODE_FAILED;

/**
 * desc-全局异常处理
 *
 * @author houmenghe
 * @date 2025/04/20 22:44
 **/
@RestControllerAdvice
public class GlobalExceptionAdvice {


    /**
     * 捕获 BusinessException 进行统一的异常处理
     */
    @ExceptionHandler(value = BusinessException.class)
    public DtoResult<Void> businessExceptionHandler(HttpServletRequest req,
                                                    BusinessException ex) {
        return new DtoResult<>(ex.getErrorCode(), ex.getMessage());
    }

    /**
     * 兜底异常捕获
     */
    @ExceptionHandler(value = Exception.class)
    public DtoResult<String> handlerException(HttpServletRequest req, Exception ex) {
        ex.printStackTrace();
        return new DtoResult<>(STATUS_CODE_FAILED, "系统异常，请联系管理人员！");
    }
}
